/*
 * @Title: 票选优品溯源图文详情
 * @Author: Jcuiweif
 * @Date:   2018-12-19
 * @Last Modified by:   Jcuiweif
 * @Last Modified time: 2018-12-19
 */
$(document).ready(function(){
  if ($(document).width()<=450){
    var t = $(document).width()/10;
    $("html").css("font-size",t);
    } else {
      $("html").css("font-size",45);
    }
  
  })
var dataInfo = {},
  template = {
    init: function() {
      Vue.use(VueLazyload, {
        preLoad: 1.3,
        error: 'https://h5.meetao.com/img/product/init.png',
        loading: 'https://h5.meetao.com/img/product/init.png',
        attempt: 1
      });
      // Vue.use(VueVideoPlayer);
      dataInfo.init = new Vue({
        el: '#app',
        data: {
          width: document.body.offsetWidth,
          tabData: [],
          swiperData: [],
          votebannerdown:[],
          versions: versions,
          icon: {
            normal: '../img/index/shouhui.png',
            active: '../img/index/shouhong.png',
            normal1: '../img/index/gouhui.png',
            active1: '../img/index/gouhong.png',
            active2: '../img/index/myhong.png',
            normal2: '../img/index/myhui.png',
            active3: '../img/index/pthong.png',
            normal3: '../img/index/pthui.png',
            downicon:"../img/voteGoods/down_icon.png",
            up_icon:"../img/voteGoods/up_icon.png",
          },
          playicon:'../img/playVideo.png',
          active: 0,
          activeindex:1,
          all: false, //loadOver - show
          isFx: 1,
          isDz: 0,
          page: 1,
          choose:0,
          limit: 10,
          loading1: 'https://h5.meetao.com/img/product/init.png',//whtiteeyes.png
          eyes:'../img/voteGoods/eyes.png',
          whtiteeyes:'../img/voteGoods/whtiteeyes.png',
          loading: false,
          isLoading: false,
          finished: false,
          listData: [], //商品集合
          coverImgUrl:'',
          sourceAuthor:'',
          videoDescUrl:'',
          videodetail:{},
          sourceTitle:'',
          reback:[],
          message: '',
          textdetail:{},
          sonPage:[],
          sonLimit:[],
          rebackindex:-1,
          showOffarr:[],
          arrson:[],
          sonLimit1:3,
          contentinput:'',
          placeholder :'我也说两句',
          moretext:'展开更多',
          type:0,
          authorId:'',
          referenceId:'',
          referenceUserId:'',
          getid:'',
          poster:'https://surmon-china.github.io/vue-quill-editor/static/images/surmon-1.jpg',
          videosrc: "http://vjs.zencdn.net/v/oceans.mp4",
          loadtext:'已加载全部',
          showOffarr:[],
          arrson:[],
          nodata:false,
          noneimg: '../img/404/404.png',
          clickOff: true,
          focusOff:false,
          grayoff:true,
          defaultAvatar:'../img/voteGoods/defaultAvatar.png'
        },
        computed:{
          // reversedMessage: function(){
          //   for (var i = 0; i < dataInfo.init.reback.length; i++) {//子评论对应评论数
          //     dataInfo.init.sonPage[i] = Math.ceil(dataInfo.init.reback[i].children.length/3);
          //   }
          //   return;
          // }
        },
        watch:{
          'contentinput':function(newVal,oldVal){
              if (newVal.length>0){
                dataInfo.init.grayoff = false;
                // dataInfo.init.grayoff = false;
              } else {
                dataInfo.init.grayoff = true;
                // dataInfo.init.grayoff = true;
              }
          }
        },
        methods: {
          onLoad: function() {
            this.sourceagain();
          },
          getFocus (){
            $('.self_talk input').get(0).focus();
            dataInfo.init.focusOff = true;
          },
          onRefresh: function() { //下拉刷新
            // this.listData = [];
            // this.page = 1;
            dataInfo.init.all = false;
            setTimeout(function() {
              dataInfo.init.sourceagain();
              // dataInfo.init.loading = false;
              // dataInfo.init.isLoading = false;
            }, 500);
          },
          bannerJump: function(data) { //bannerJump
            if (data.linkRemark == 'product') { //商品 文章 视频 均为跳转内部地址 h5为跳转全链接地址
              jumpProductDetails(data.params);
            } else if (data.linkRemark == 'h5') {
              window.location.href = data.link;
            } else if (data.linkRemark == 'article') {

            } else if (data.linkRemark == 'video') {

            };
          },
          sourceagain:function(){
            ajaxTemplate(addressTest+SERVICE.sourcetalk, function(data) {
              var daTa = data.data.result.pageData;
              var arr = [];
              var arr1 = [];
              console.log(daTa)
              if (!daTa || daTa.length == 0){
                // dataInfo.init.$toast('全部加载完');
                if (dataInfo.init.reback.length ==0){
                  dataInfo.init.nodata= true;
                } else {
                  dataInfo.init.all= true;
                }
                dataInfo.init.loading = false;
                dataInfo.init.isLoading = false;
                dataInfo.init.finished = true;
                return;
              }
              // console.log(dataInfo.init.finished,dataInfo.init.loading,dataInfo.init.isLoading,daTa,dataInfo.init.sonPage)
              if (data.tag == 0) {
                dataInfo.init.reback = JSON.parse(JSON.stringify(dataInfo.init.reback.concat(daTa)));
                for (var s = 0;s<daTa.length;s++){
                    if (daTa[s].children.length>=3){
                     arr[s] = daTa[s].children.slice(0,3);
                    } else {
                      arr[s] = daTa[s].children.slice(0,daTa[s].children.length+1);
                    } 
                    arr1[s] = true;
                }
                dataInfo.init.arrson = JSON.parse(JSON.stringify(dataInfo.init.arrson.concat(arr)));//子评论对应评论数
                dataInfo.init.showOffarr = JSON.parse(JSON.stringify(dataInfo.init.showOffarr.concat(arr1)));
                if (daTa.length < dataInfo.init.limit) {
                  console.log("无数据")
                  dataInfo.init.finished = true;
                  dataInfo.init.all = true;
                } else {
                  console.log('有数据')
                  dataInfo.init.finished = false;
                };
                if (dataInfo.init.reback.length<10){
                  dataInfo.init.finished = true;
                  dataInfo.init.all = true;
                }
                if (dataInfo.init.reback.length==0){
                  dataInfo.init.nodata = true;
                  dataInfo.init.all = false;
                }
                dataInfo.init.loading = false;
                dataInfo.init.isLoading = false;
              } else {
                dataInfo.init.$toast(data.errMsg);
                if (data.errMsg == '未查询到符合条件的商品'){
                  dataInfo.init.loading = false;
                  dataInfo.init.isLoading = false;
                  dataInfo.init.all = true;
                  dataInfo.init.finished = true;
                }
              };
            }, {
              id :dataInfo.init.getid,
              themeType:'trace',
              page:dataInfo.init.page++,
              limit:dataInfo.init.limit,
              // sonPage:1,
              // sonLimit:3,
              userId:userId
            });
          },
          bulrInput (){
            if (dataInfo.init.grayoff){
              dataInfo.init.$set(dataInfo.init,'contentinput','');
              dataInfo.init.focusOff = false;
              dataInfo.init.type = 0;
              dataInfo.init.referenceUserId = dataInfo.init.getid;
              dataInfo.init.referenceId =dataInfo.init.getid;
              dataInfo.init.$set(dataInfo.init,'placeholder','我也说两句');
            }
            // setTimeout(function(){
              window.scrollTo(0, document.body.scrollTop + 1);
              document.body.scrollTop >= 1 && window.scrollTo(0, document.body.scrollTop - 1);  
            // }, 10)
          },
          bulrIn (){
            // if (dataInfo.init.grayoff){
              dataInfo.init.$set(dataInfo.init,'contentinput','');
              dataInfo.init.focusOff = false;
              dataInfo.init.type = 0;
              dataInfo.init.referenceUserId = dataInfo.init.getid;
              dataInfo.init.referenceId =dataInfo.init.getid;
              dataInfo.init.$set(dataInfo.init,'placeholder','我也说两句');
            // }
            $('.self_talk input').get(0).blur();
              dataInfo.init.focusOff = false;
          },
          bannerJump: function(data) { //bannerJump
            if (data.linkRemark == 'product') { //商品 文章 视频 均为跳转内部地址 h5为跳转全链接地址
              jumpProductDetails(data.params);
            } else if (data.linkRemark == 'h5') {
              window.location.href = data.link;
            } else if (data.linkRemark == 'article') {

            } else if (data.linkRemark == 'video') {

            };
          },
          bannerJump2: function(data) { //bannerJump
            console.log(data)
            if (data.linkRemark == 'product') { //商品 文章 视频 均为跳转内部地址 h5为跳转全链接地址
              jumpProductDetails(data.params);
            } else if (data.linkRemark == 'h5') {
              window.location.href = data.h5JumpUrl;
            } else if (data.linkRemark == 'article') {

            } else if (data.linkRemark == 'video') {

            };
          },
          toProduct: function(data) { //toProduct
            jumpProductDetails(data);
          },
          tabJump: function(data) {
            location.href = "../html/productClassify.html?code=" + data.code + "&name=" + escape(data.name);
          },
          getProductDetails: function() { //获取商品数据
            ajaxTemplate(addressUrl + SERVICE.queryFxGoodsList, function(data) {
              console.log(data);
              var daTa = data.data.result;
              if (data.tag == 0) {
                dataInfo.init.listData = JSON.parse(JSON.stringify(dataInfo.init.listData.concat(daTa)));
                if (daTa.length < dataInfo.init.limit) {
                  console.log("无数据")
                  dataInfo.init.finished = true;
                  dataInfo.init.all = true;
                } else {
                  console.log('有数据')
                  dataInfo.init.finished = false;
                };
                dataInfo.init.loading = false;
                dataInfo.init.isLoading = false;
                //					是否要有秒杀
                //					daTa.forEach(function(item, index) {
                //						if(item.isKill !== 0) {
                //							dataInfo.init.listData.push(item);
                //						};
                //					});
                //					setTimeout(function() {
                //						var tid = "index" + (daTa.length - 1);
                //						$('#' + tid).ready(function() {
                //							$(".loadImgBox").addClass("rgba").hide();
                //						});
                //					}, 300);
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              limit: dataInfo.init.limit,
              page: dataInfo.init.page++,
            });
          },
          itemclick (index){//一级评论
            $('.self_talk input').get(0).focus();
            dataInfo.init.focusOff = true;
            var nickname = dataInfo.init.reback[index].nickName;
            dataInfo.init.type = 1;
            dataInfo.init.$set(dataInfo.init,'contentinput','');
            dataInfo.init.referenceUserId = dataInfo.init.reback[index].authorId;//被评论者userid
            console.log(dataInfo.init.referenceUserId)
            dataInfo.init.rebackindex = index;
            dataInfo.init.referenceId = dataInfo.init.reback[index].id;//被评论组一级id
            dataInfo.init.$set(dataInfo.init,'placeholder','回复:'+nickname);
          },
          catchevent(e){
            // console.log(e. )
            if(e.target.id != 'sub'){
              dataInfo.init.$set(dataInfo.init,'contentinput','');
              dataInfo.init.focusOff = false;
              dataInfo.init.type = 0;
              dataInfo.init.referenceUserId = dataInfo.init.getid;
              dataInfo.init.referenceId =dataInfo.init.getid;
              dataInfo.init.$set(dataInfo.init,'placeholder','我也说两句');
            }
            
            if(e.target.innerText == '发布'&&!dataInfo.init.grayoff&&dataInfo.init.contentinput){
              console.log(e.target,e.target.innerText,dataInfo.init.grayoff,dataInfo.init.contentinput)
              dataInfo.init.submit(e);
            }
            if (e.target.nodeName == 'INPUT'){
              $('.self_talk input').get(0).focus();
               dataInfo.init.focusOff = true;
            } else {
              // $('.self_talk input').get(0).blur();
              // dataInfo.init.focusOff = false;
              
            }
          },
          cloneclick(index,sle) {//二级评论
            $('.self_talk input').get(0).focus();
            dataInfo.init.focusOff = true;
            var nickname = dataInfo.init.reback[index].children[sle].nickName1;
            dataInfo.init.type = 1;
            dataInfo.init.$set(dataInfo.init,'contentinput','');
            dataInfo.init.rebackindex = index;
            // console.log(dataInfo.init.reback[index].children[sle].id)
            dataInfo.init.referenceUserId = dataInfo.init.reback[index].children[sle].authorId;//被评论者id
            // console.log(dataInfo.init.referenceUserId)
            dataInfo.init.referenceId = dataInfo.init.reback[index].id;//被评论组一级id
            dataInfo.init.$set(dataInfo.init,'placeholder','回复:'+nickname);
          },
          submit(event){
            if (dataInfo.init.grayoff) return;
            if (!dataInfo.init.clickOff) return;
            if (dataInfo.init.contentinput.trim().length == 0){
              dataInfo.init.type = 0;
              dataInfo.init.referenceUserId = dataInfo.init.getid;
              dataInfo.init.referenceId =dataInfo.init.getid;
              dataInfo.init.contentinput = '';
              dataInfo.init.$toast('评论内容不能为空');
              dataInfo.init.$set(dataInfo.init,'placeholder','我也说两句');
              return;
            }
            dataInfo.init.clickOff = false;
            console.log(dataInfo.init.contentinput,dataInfo.init.referenceUserId,dataInfo.init.referenceId,dataInfo.init.type,userId)
            ajaxTemplate(addressTest+SERVICE.sourceadd, function(data) {
              console.log('sew',data)
              dataInfo.init.clickOff = true;
              dataInfo.init.focusOff = false;
              dataInfo.init.grayoff = false;
                if (data.tag == 0) {
                    // $('.textdetail').html(result.sourceTextDesc);
                    dataInfo.init.$toast('评论成功');
                    dataInfo.init.nodata = false;
                    dataInfo.init.contentinput = '';
                    dataInfo.init.referenceUserId = dataInfo.init.getid;
                    dataInfo.init.referenceId =dataInfo.init.getid;
                    // console.log(dataInfo.init.reback[dataInfo.init.rebackindex].children.length)
                    if (dataInfo.init.type == 0){
                      // console.log(data.data.commentResult.concat(dataInfo.init.reback))
                      data.data.commentResult[0].children=[];
                      dataInfo.init.$set(dataInfo.init,'reback',data.data.commentResult.concat(dataInfo.init.reback));
                      dataInfo.init.arrson.unshift([]);
                      dataInfo.init.showOffarr.unshift(true);
                    } else {
                      if (dataInfo.init.reback[dataInfo.init.rebackindex].children.length>3){
                        dataInfo.init.reback[dataInfo.init.rebackindex].children.push(data.data.commentResult[0]);
                        // console.log(dataInfo.init.showOffarr[dataInfo.init.rebackindex])
                        if (dataInfo.init.showOffarr[dataInfo.init.rebackindex] == false){
                          // dataInfo.init.arrson[dataInfo.init.rebackindex].push(data.data.commentResult[0]);
                        }
                        // console.log(dataInfo.init.reback[dataInfo.init.rebackindex],dataInfo.init.arrson[dataInfo.init.rebackindex])
                      } else if (dataInfo.init.reback[dataInfo.init.rebackindex].children.length<=2)  {
                        dataInfo.init.reback[dataInfo.init.rebackindex].children.push(data.data.commentResult[0]);
                        dataInfo.init.arrson[dataInfo.init.rebackindex].push(data.data.commentResult[0]);
                      } else if (dataInfo.init.reback[dataInfo.init.rebackindex].children.length == 3)  {
                        dataInfo.init.reback[dataInfo.init.rebackindex].children.push(data.data.commentResult[0]);
                      }
                    }
                    dataInfo.init.type = 0;
                    dataInfo.init.$set(dataInfo.init,'placeholder','我也说两句');
                    // console.log(dataInfo.init.arrson[dataInfo.init.rebackindex],dataInfo.init.reback[dataInfo.init.rebackindex].children,dataInfo.init.showOffarr[dataInfo.init.rebackindex])
                    
                } else {
                    dataInfo.init.$toast(data.errMsg);
                };
                }, {
                   content:dataInfo.init.contentinput,
                   referenceUserId: dataInfo.init.referenceUserId,//被评论人id默认溯源id
                   themeId: dataInfo.init.getid,//默认溯源id

                   referenceId: dataInfo.init.referenceId,//被评论人id一级默认溯源id

                   type:dataInfo.init.type,
                   authorId:userId,
                   themeType:'trace',
                   userId:userId
                });
          },
          morechild(index){
            console.log(index)
            dataInfo.init.moretext = '收起列表';
            dataInfo.init.$set(dataInfo.init.showOffarr,index,false);
            dataInfo.init.$set(dataInfo.init.arrson,index,dataInfo.init.reback[index].children);
            // console.log(dataInfo.init.arrson)
            // dataInfo.init.showOffarr[index] = false;
            // dataInfo.init.arrson[index] = dataInfo.init.reback[index].children;
            // var s = Number(dataInfo.init.sonPage[index])+1;
            // dataInfo.init.$set(dataInfo.init.sonPage,index,s);
            // console.log(dataInfo.init.reback[index].id,dataInfo.init.limit,dataInfo.init.sonPage[index]);
            // ajaxTemplate(addressTest+SERVICE.searchid, function(data) {
            //   // console.log('sew',data)
            //   showOff = true;
            //   var data = data;
            //     if (data.tag == 0) {
            //         dataInfo.init.reback[index].children = JSON.parse(JSON.stringify(dataInfo.init.reback[index].children.concat(data.data.result.pageData)))
            //         // console.log(dataInfo.init.reback[index].children);
            //     } else {
            //         dataInfo.init.$toast(data.errMsg);
            //     };
            //     }, {
            //        commentid:dataInfo.init.reback[index].id,//查询一级评论id
            //        sonLimit:dataInfo.init.sonLimit1,
            //        sonPage:dataInfo.init.sonPage[index],
            //        themeType:'trace',
            //        userId:userId
            //     });
          },
          reduce(index){
            // dataInfo.init.reback[index].children = JSON.parse(JSON.stringify(dataInfo.init.reback[index].children.slice(0,3)));
            // dataInfo.init.showOffarr[index] = false;
            dataInfo.init.moretext = '展开更多';
            dataInfo.init.$set(dataInfo.init.showOffarr,index,true);
            dataInfo.init.$set(dataInfo.init.arrson,index,dataInfo.init.reback[index].children.slice(0,3));
          },
          getSource: function() {
            // if (location.href.indexOf('&')>-1){
            //   var title = location.href.split('&')[1].split('=')[1];
            //   shareInfo.title  = decodeURIComponent(title);
            //   dataInfo.init.$set(dataInfo.init,'sourceTitle',decodeURIComponent(title));
            // }
            var id =decodeURIComponent(getQueryString('id')); 
            dataInfo.init.getid = id;
            // ajaxTemplate(addressTest+SERVICE.sourcetalk, function(data) {
            //   console.log('wtry',data)
            //     if (data.tag == 0) {
            //         var result = data.data.result.pageData;
            //         console.log(data)
            //         dataInfo.init.$set(dataInfo.init,'reback',result);
            //         // $('.textdetail').html(result.sourceTextDesc);
            //     } else {
            //         dataInfo.init.$toast(data.errMsg);
            //     };
            //     }, {
            //        id :id,
            //        themeType:'trace',
            //        page:dataInfo.init.page,
            //        limit:dataInfo.init.limit,
            //        sonPage:1,
            //        sonLimit:3,
            //        userId:userId
            //     });
              ajaxTemplate(addressTest + SERVICE.sourceinfo, function(data) {
                if (data.tag == 0) {
                    var result = data.data.result;
                    console.log('video',data)
                    dataInfo.init.referenceId = result.id;
                    dataInfo.init.referenceUserId = result.id;
                    dataInfo.init.$set(dataInfo.init,'videodetail',result);
                    shareInfo.imgUrl = result.shareImgUrl;
                    shareInfo.voteActivityCode = result.voteActivityCode;
                    shareInfo.voteItemCode = result.voteItemCode;
                    shareInfo.stitle = result.sourceTitle.toString();
                } else {
                    dataInfo.init.$toast(data.errMsg);
                };
                }, {
                    sourceCode:getQueryString('sourceCode'),
                });
               
          },
        },
      });
      dataInfo.init.getSource();
    //   dataInfo.init.getTab();
    }
  };
template.init();
